package com.cloudbroker.bcs.csvc.sms.hsiuc.service;

import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.Map;

import com.cloudbroker.bcs.csvc.itn.api.constants.ITNFields;
import com.cloudbroker.bcs.csvc.sms.hsiuc.constants.HSIUCErrorCodes;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.ConnectTimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.cloudbroker.bcs.csvc.sms.api.constants.SMSFields;
import com.cloudbroker.bcs.csvc.sms.api.constants.SMSImplTypes;
import com.cloudbroker.bcs.csvc.sms.common.service.SMSServiceAbstractImpl;
import com.cloudbroker.bcs.csvc.sms.common.util.SMSServiceHelper;
import com.cloudbroker.bcs.csvc.sms.hsiuc.constants.HSIUCConstants;
import com.cloudbroker.bcs.csvc.sms.hsiuc.constants.HSIUCFields;
import com.cloudbroker.bcs.csvc.sms.hsiuc.util.HSIUCHelper;

@Service(SMSImplTypes.HSIUC_HTTP)
@Lazy
public class SMSServiceHSIUCHttpImpl extends SMSServiceAbstractImpl {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SMSServiceHSIUCHttpImpl.class);
    
    @Override
    public void sendSingle(Map<String, Object> param, Map<String, Object> result) {
        String remoteServiceUrl = SMSServiceHelper.removeParamRequiredAsString(param,
                HSIUCFields.CONFIG_PARAM_SVCURL);
        String ownCompanyId = SMSServiceHelper.removeParamRequiredAsString(param,
                HSIUCFields.CONFIG_PARAM_OWNCOMPANYID);
        String companyId = SMSServiceHelper.removeParamRequiredAsString(param, HSIUCFields.CONFIG_PARAM_COMPANYID);
        String businsysNo = SMSServiceHelper
                .removeParamRequiredAsString(param, HSIUCFields.CONFIG_PARAM_BUSINSYSNO);
        String smsPrioriry = SMSServiceHelper.removeParamRequiredAsString(param,
                HSIUCFields.CONFIG_PARAM_SMSPRIORITY);
        
        String mobileTel = SMSServiceHelper.getParamRequiredAsString(param, SMSFields.MOBILE);
        String content = SMSServiceHelper.getParamRequiredAsString(param, SMSFields.CONTENT);
        String auth_key = String.valueOf(param.get(HSIUCFields.AUTH_KEY));

        LOGGER.info("send to " + mobileTel);
        
        Map<String, Object> request = new HashMap<String, Object>();
        request.put(HSIUCFields.MOBILE_TEL, mobileTel);
        request.put(HSIUCFields.SMS_CONTENT, content);
        request.put(HSIUCFields.OWN_COMPANY_ID, ownCompanyId);
        request.put(HSIUCFields.COMPANY_ID, companyId);
        request.put(HSIUCFields.BUSINSYS_NO, businsysNo);
        request.put(HSIUCFields.SMS_PRIORITY, smsPrioriry);
        if(StringUtils.isNotBlank(auth_key)){
            request.put(ITNFields.SMS_AUTHKEY,auth_key);
        }else {
            LOGGER.info(ITNFields.SMS_AUTHKEY+"为空");
        }
        try {
            JSONObject response = HSIUCHelper.getHttpClient().postFormFetchJSON(remoteServiceUrl, request,
                    "sendSMSSingle", HSIUCConstants.CHARSET);
            if (HSIUCErrorCodes.isSuccess(response)) {
                JSONObject data = response.getJSONObject(HSIUCFields.DATA);
                if (null != data) {
                    result.put(SMSFields.SEND_CHN_MSG_ID, data.getString(HSIUCFields.SMS_ID));
                }
                SMSServiceHelper.markSuccess(result);
                LOGGER.info("send to " + mobileTel + " success");
            } else {
                String errorCode = response.getString(HSIUCFields.ERROR_CODE);
                String errorInfo = response.getString(HSIUCFields.ERROR_INFO);
                SMSServiceHelper.markRemoteServiceBizError(result, "send sms failed", errorCode, errorInfo);
                LOGGER.info("send to " + mobileTel + " failed: " + result);
            }
        } catch (ConnectTimeoutException e) {
            LOGGER.error("send single sms failed", e);
            SMSServiceHelper.markRemoteServiceAccessError(result, "access remote service connect timeout");
        } catch (SocketTimeoutException e) {
            LOGGER.error("send single sms failed", e);
            SMSServiceHelper.markRemoteServiceAccessError(result, "access remote service socket timeout");
        } catch (Exception e) {
            LOGGER.error("send single sms failed", e);
            SMSServiceHelper.markCommonError(result);
        }
        
    }
    
}
